Thanks SJH, but I only needed to get the array length once, so Tom's method worked fine.
I've pasted as far as my code got below, as I'll no longer need it. While establishing the compensation values, the spindle motor started acting up, and as I was considering a servo motor anyway, a shiny new servo spindle/drive has been ordered.
There is one major flaw in it, in that if a speed/voltage greater than the second last value in the array is commanded, it sets 0V. It was next on my to fix list having established the compensation/interpolation was working correctly, but now I don't need it, I've got more pressing things needing my time. So I'm just posting this code here as a basis for anybody looking for an interpolation example, as I couldn't find anything specific to the KFlop.
Although having just looked over the code, it may be just a case of either adding an = to the less than in the if, or dropping the -2 in the if... (I'd test it, but I've not got a KFlop handy!).
I had to chop and change the if statement a few times, before realising I couldn't declare the int within the if statement itself, so I may of inadvertently deleted something I didn't mean to...
Moray
float *SpinVolt = &persist.UserData[SPDLVOLT]; // User persist used to set compensated voltage required
float *SpinSpeed = &persist.UserData[SPDLSPEED]; // User persist where we receive commanded speed from KMotionCNC S command
static int SpinMaxSpeed = 2500; // Max spindle speed I.e. value at which we want maximum speed/voltage
// The following array has the structure of Desired voltage, Commanded voltage
// to generate this array, change speed command until requested voltage is established
// I.e. to get the desired 1V, in this uneditted file, I needed to request 425RPM which gives 1.70V
// (Keep the console screen open in KMotion, and these values are displayed)
static float SpinCorr[] = {0,0,
1,1.70,
2,2.64,
3,3.50,
4,3.98,
5,5.15,
6,5.92,
7,6.7,
8,7.5,
9,8.35,
10,9.25};
main()
{
float RawVolt = (*SpinSpeed/SpinMaxSpeed)*10; // calculate desired voltage
printf("Spindle Speed = %f\n",*SpinSpeed);
int arrLen = sizeof(SpinCorr)/sizeof(double); // get size of array
int i= 0;
float newVolt;
for(i; i<arrLen-2; i=i+2)
{
if ((RawVolt >= SpinCorr[i]) && (RawVolt <= SpinCorr[i+2])) {
// calculate compensated voltage
newVolt = SpinCorr[i+1] + ((SpinCorr[i+3]-SpinCorr[i+1]) * ((RawVolt-SpinCorr[i]) / (SpinCorr[i+2]-SpinCorr[i])));
break;
}
}
printf("Voltage Raw=%f Compensated=%f\n",RawVolt, newVolt);
*SpinVolt = newVolt; // Store compensated voltage in relevant user persist
}